/*
 * SocketConnection.java
 */

/* Copyright information:
 * 
 *  Copyright 2010 Failboat Productions. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 * 
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY FAILBOAT PRODUCTIONS ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL FAILBOAT PRODUCTIONS OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the
 * authors and should not be interpreted as representing official policies, either expressed
 * or implied, of Failboat Productions.
 */

package GUI;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class SocketConnection {
	private String url;
	private boolean isHost;
	private final int PORT = 11250;
	private ServerSocket host; 
	private Socket connection;
	private ObjectInputStream input;
	private ObjectOutputStream output;
	
	
	/**
	 * Create a SocketConnection. 
	 * @param url
	 * @param host True if host of connection.
	 */
	public SocketConnection(String url, boolean host){
		this.url = url;
		isHost = host;
		
		/* Setting values to null */
		this.host = null;
		connection = null;
		input = null;
		output = null;
	}
	
	
	/**
	 * Establish a hosting connection, wait for connection
	 * @return True if connection is ok.
	 */
	public boolean getConnection(){
		if(connection == null){
			try {
				if(isHost){
					host = new ServerSocket(PORT);
					connection = host.accept();
				}else{
					connection = new Socket(url, PORT);
				}				
				return true;
			}catch (Exception e) {
				e.printStackTrace();
				return false;
			}
		}else{
			if(connection.isConnected()){
				return true;
			}else{
				return false;
			}
		}
	}
	
	
	/**
	 * Get an object input stream.
	 * @return
	 */
	public ObjectInputStream getInput(){
		if (input == null && connection != null){
			try{
		
				input = new ObjectInputStream(connection.getInputStream());
				return input;
			}catch (Exception e) {
				return null;
			}
		}else if(connection == null){
			return null;
		}else{
			return input;
		}
	}
	
	
	/**
	 * Get an objectoutput stream
	 * @return
	 */
	public ObjectOutputStream getOutput(){
		if (output == null && connection != null){
			try{
				output = new ObjectOutputStream(connection.getOutputStream());
				output.flush();
				return output;
			}catch (Exception e) {
				return null;
			}
		}else if(connection == null){
			return null;
		}else {
			return output;
		}
	}
	
	
	/**
	 * Closes input, output and socket
	 */
	public void closeAll(){
		try{
			output.close();
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			try{
				input.close();
			}catch (Exception e) {
				e.printStackTrace();
			}finally{
				try{
					connection.close();
				}catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
	}
}
